<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
class Integrationrule extends Controller{
    /****
     * Class register          	积分规则
     * @$integral[int]   		钱数或积分
     * @$user_id[int]			用户id
     * @$type[int]      		类型1生成积分，0消费积分
     * @$content[int]           说明
     * @$order_number[int]		订单号
     * @date                	2020/05/19 
     * @package application\index\controller\Integrationrule
    */
    public function index($integral,$user_id,$type,$content,$order_number){
    	$ratio = Db::name('ratio')->where(['type'=>1])->find();
    	$rule = $integral*($ratio['ratio']/100);
    	$data = [
    		"user_id"	=>$user_id,
    		"time"		=>time(),
    		'type'		=>$type,
    		'content'	=>"购买产品：".$integral."订单号".$order_numbe."积分！",
        		'integral'	=>$rule
    	];
    	Db::name('credits_log')->insert($data);
    	$user = Db::name('user')->find($user_id);
    	if($type == 1){
    		Db::name('user')
    		->update(['score' => $user['score'] + $rule ,'id'=>$user['id']]);
    	}else{
    		if($integral > $user['score']){
    			$arr = [
			       'state'     => 0,
			       'message'  => "积分不足！"
	       		];
	       		return json($arr);
    		}
    		Db::name('user')
    		->update(['score' => $user['score'] - $integral ,'id'=>$user['id']]);
    	}
    }    
}
